	
require('../../../lib/md5/md5.js');

define(['vue','config','common'],function(Vue,config,Utils){
	'use strict';

	var loginView = new Vue({
		el:'#app',
		data:{
			role:'E享会员',
			customerID:5,
			userID:4,
			phone:''

		},
		created:function(){
			var _this=this;
			//Utils.clearData();

			var phone = Utils.getData('phone');
			if(phone){
				_this.phone = Utils.getData('phone');
			}

			/*var role = Utils.getData('role');
			if(role){
				_this.role = role;
			}else{
				_this.role = 'E享会员';
			}*/
			
			//获取用户类型
			/*Utils.initAjax('user/getUserType',null,'POST',function(res){
				//console.log(res)
				_this.userID = res.data[1].id;
				_this.customerID = res.data[2].id;
			})*/
		},
		methods:{
			/*SelectRole:function(event){
				$('.Pop_BJ').show();
			},
			Role:function(event){
				this.role=$(event.target).text();
				$('.Pop_BJ').hide();
			},*/
			pswfocus:function(){
				$('.loginCopy').hide();
			},
			pswblur:function(){
				$('.loginCopy').show();
			},
			focus:function(){
				var phone = $('#phone').val();
				$('.loginCopy').hide();
				if($.trim(phone)!==""){
					$('.delect').fadeIn(300);
				}
			},
			keyup:function(event){
				var phone = $('#phone').val();

				if($.trim(phone)!==""){
					$('.delect').fadeIn(300);
				}else{
					$('.delect').fadeOut(300);
				}			
			},
			blur:function(){
				$('.delect').fadeOut(300);
				$('.loginCopy').show();
			},
			delect:function(){
				$('#phone').val('');
				$('.delect').fadeOut(300);
				var phone = $('#phone').val();
				if($.trim(phone) == ""){
					$('#phone').focus();
				}
			},
			submit:function(event){
				var role = $('#role').text();
				var phone = this.phone;
				var psw = $('#psw').val();

				if($.trim(phone) === ""){
                    Utils.appAlert('手机号码不能为空',function(){
                    	$('#phone').focus();
                    });
                    return;
				}

				if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
                    Utils.appAlert('请输入正确的手机号码',function(){
                    	$('#phone').focus();
                    });
                    return;
				}

				Utils.initAjax('user/isEixt',{
					phone: this.phone
				},'POST',function(res){
					if(res.msg == "false"){
						Utils.appAlert('此用户不存在');
					}else{
						if($.trim(psw) === ""){
		                    Utils.appAlert('密码不能为空',function(){
		                    	$('#psw').focus();
		                    });
		                    return;
						}

						if(!(/^.{6,20}$/.test(psw))){
							Utils.appAlert('密码只能在6~20位之间',function(){
		                    	$('#psw').focus();
		                    });
		                    return;
						}

						var _this=this;
						/*var userTypeId;
						if(role.indexOf('会员') === -1){
							userTypeId = _this.customerID;
						}else{
							userTypeId = _this.userID;
						}*/
						var url= Utils.getWebUrl('user/login');
						var data={
							loginName:phone,
							password:$.md5(psw),
							/*userTypeId:userTypeId*/
						};

						$.ajax({
			                url: url,
			                type: 'POST',
			                data: data,
			                dataType: 'json',
			                xhrFields: {
			                    withCredentials: true
			                },
			                crossDomain: true,
			                beforeSend:function(){
			                	$('.DataLoading').show();
			                },
			                complete:function(){
			                	$('.DataLoading').hide();
			                },
			                success: function(res) {
			                	if(res.code === "10000"){
			                		Utils.clearData();
									//var userInfo = res.data.data;
									//Utils.setData('userInfo',userInfo);
									//缓存
									Utils.setData('phone',phone);
									//Utils.setData('role',role);
									//Utils.setData('userTypeId',res.data.data.userTypeId);

									var status = res.data.data.shopsStatus*1;
									var token = res.data.token;

									Utils.userToken(token);

									window.location.href = "userInfo.html";	
									
			                	}else{
			                		Utils.appAlert(res.msg);
			                	}
			                },
			                error: function(res) {
			                    Utils.appAlert('系统出现了错误,系统管理员正在修复');
			                }
			            });
					}
				})
				
			}
		}
	})

	return loginView;
});